package com.ruoyi.api.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.result.ResultView;
import com.ruoyi.system.domain.SysAssetsVehicle;
import com.ruoyi.system.service.ISysAssetsVehicleService;
import com.ruoyi.util.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * Author: lvaitly
 * Time: 15:17
 * Desc: 车辆相关
 */
@RestController
@RequestMapping("/api/vehicle")
@Slf4j
public class VehicleController {
    @Value("${wspaas.wsVehicleUrl}")
    private String vehicleUrl;
    @Autowired
    private ISysAssetsVehicleService sysAssetsVehicleService;

    //VehicleDaily 获取车辆指定日作业情况, GetVehicleTrack 获取车辆作业轨迹, GetVehicleTrackRT 获取车辆实时位置
    @PostMapping("/lbs/{service}")
    private ResultView wsVehicleService(@PathVariable String service, @RequestBody JSONObject o) {
        if (null == o || null == o.getString("carNo")) {
            return ResultView.error(-1, "invalid param");
        }
        StringBuilder sb = new StringBuilder();
        sb.append(vehicleUrl).append("/KeChuangVehicle.asmx/").append(service);
        String s = HttpClientUtil.doPost(sb.toString(), o);
        if (!"-1".equalsIgnoreCase(s)) {
            JSONObject result = JSON.parseObject(s);
            if (null != result && 0 == result.getInteger("code")) {//成功
                if (null != result.getJSONArray("data")) {
                    return ResultView.ok(result.getJSONArray("data").get(0));
                }
                return ResultView.ok();
            }
            log.error(s);
        }
        return ResultView.error(-1, "fail");
    }

    //获取车辆信息
    @PostMapping("/get/baseinfo")
    public ResultView getVehicleInfo(@RequestBody JSONObject o){
        if(null==o||null==o.get("carNo")){
            return ResultView.error(-1,"invalid param");
        }
        SysAssetsVehicle r=sysAssetsVehicleService.selectSysAssetsVehicleByPlateNo(o.getString("carNo"));
        if(null==r){
            return ResultView.error(-1,"not exist");
        }
        return ResultView.ok(r);
    }

    @PostMapping("/list/{uid}")
    public ResultView getVehicleListByUid(@PathVariable String uid){
        return ResultView.ok(sysAssetsVehicleService.selectSysAssetsVehicleByUid(uid));
    }

    //绑定车辆
    @PostMapping("/update/bind")
    public ResultView getVehicleListByUid(@RequestBody SysAssetsVehicle vo){
        if(null==vo.getId()||null==vo.getUid()||null==vo.getUsers()){
            return ResultView.error(-1,"fail");
        }
        SysAssetsVehicle asset=new SysAssetsVehicle();
        asset.setId(vo.getId());
        asset.setUid(vo.getUid());
        asset.setUsers(vo.getUsers());
        int i= sysAssetsVehicleService.updateSysAssetsVehicle(asset);
        if(i>0){
            return ResultView.ok();
        }
        return ResultView.error(-1,"fail");
    }
}
